#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int Maxn=1e6+7,N=1e5+5;
int n,f[N][510],a[Maxn],T,b[Maxn];

int main(){
    freopen("game.in","r",stdin);
    freopen("game.out","w",stdout);
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=n;i++) scanf("%d",&b[i]);
        bool tag=0;
        for(int i=1;i<=n;i++) if(!a[i] and b[i]) tag=1;
        if(tag){
            puts("-1");
            continue;
        }
        memset(f,0x3f,sizeof f);
        f[0][0]=0;
        for(int i=1;i<=n;i++){
            if(a[i]>=b[i]){
                if(!b[i]){
                    int mx=2e9;
                    for(int j=505;j>=a[i];j--) mx=min(mx,f[i-1][j]),f[i][j]=mx;
                    int mn=2e9;
                    for(int j=0;j<a[i];j++) mn=min(mn,-j+f[i-1][j]);
                    for(int j=a[i];j<=505;j++) f[i][j]=min(f[i][j],j+mn);
                }
                else{
                    for(int j=0;j<=505;j++) f[i][a[i]-b[i]]=min(f[i][a[i]-b[i]],f[i-1][j]+max(0,a[i]-b[i]-j));
                }
            }
            else{
                for(int j=0;j<=505;j++) f[i][0]=min(f[i][0],f[i-1][j]);
            }
        }
        int ans=f[0][1];
        for(int i=0;i<=505;i++) ans=min(ans,f[n][i]);

        memset(f,0x3f,sizeof f);
        f[0][0]=0;
        for(int i=1;i<=n;i++){
            if(a[i]<b[i]){
                for(int j=0;j<=505;j++) f[i][b[i]-a[i]]=min(f[i][b[i]-a[i]],f[i-1][j]+max(0,b[i]-a[i]-j));
            }
            else{
                if(!b[i]){
                    for(int j=0;j<=505;j++) f[i][j]=f[i-1][j];
                }
                else{
                    for(int j=0;j<=505;j++) f[i][0]=min(f[i][0],f[i-1][j]);
                }
            }
        }
        int ret=f[0][1];
        for(int i=0;i<=505;i++) ret=min(ret,f[n][i]);

        printf("%d\n",ans+ret);
    }

    // system("pause");
    return 0;
}
/*
3
5
1 1 1 1 1
2 0 2 0 2
6
1 1 4 5 1 4
1 9 1 9 8 10
8
2 0 0 4 1 1 1 5
2 0 0 5 0 1 1 7
*/